<template>
  <view class="my-wrap">
    <view class="header">
      <view class="login">
        <img class="avatar-img" :src="avatarUrl" />
        <view class="header-word">
          <view class="text-one">
            {{ nickName || "用户昵称" }}
          </view>
          <view class="text-wrapper">
            <view class="text-two">已经在ladder学习进步了100天</view>
          </view>
        </view>
      </view>
    </view>
    <view class="card info">
      <view class="card-title">
        <text class="title">个人战绩</text>
      </view>
      <view class="bottom-card">
        <view class="square share">
          <text class="title">赛季 {{currentSeason.order + 1}} 总场次</text>
          <text class="subtitle">{{ formatNum(totalCount) }}</text>
        </view>
        <view class="square points">
          <text class="title">赛季 {{currentSeason.order + 1}} 积分</text>
          <text class="subtitle">{{ formatNum(totalPoints) }}</text>
        </view>
      </view>
    </view>
    <view class="card entry-list">
      <u-cell-group>
        <u-cell-item
          title="赛季详情"
          icon="integral-fill"
          :arrow="false"
        ></u-cell-item>
        <u-cell-item
          icon="setting-fill"
          title="活动"
          :arrow="false"
        ></u-cell-item>
      </u-cell-group>
    </view>
  </view>
</template>

<script lang="ts">
import { Vue, Component, Mixins } from "vue-property-decorator";
import { loginMixin } from "../../mixins";
import { Season } from '@/types/season';

@Component({
  components: {},
})
export default class extends Mixins(loginMixin) {
  totalPoints: number = 0;

  totalCount: number = 0;

  avatarUrl: string = "";

  nickName: string = "";

  get userInfo() {
    return this.$$store.userInfo;
  }

  public get currentSeason(): Season {
    return this.$$store.seasons[0];
  }

  onShow() {
    this.init();
  }

  init() {
    const that = this;
    wx.showLoading({ title: "别急哦...", mask: true });
    // 调用云函数
    wx.cloud
      .callFunction({
        name: "get_user_info",
      })
      .then(({ result }) => {
        wx.hideLoading();
        console.log("[云函数] [get_user_info] 调用成功: ", result);
        if (Array.isArray(result) && result.length > 0) {
          const userContent: any = result.filter(
            (item: any) => item.id === this.userInfo.id
          );
          this.avatarUrl = userContent[0].avatarUrl;
          this.nickName = userContent[0].nickName;
          this.totalCount = userContent[0].activities.filter((item: any) => item.seasonId === this.currentSeason._id).length;
          this.totalPoints = userContent[0].activities
            .filter((item: any) => item.seasonId === this.currentSeason._id)
            .reduce((prev: any, next: any) => {
              return prev + Number(next.score);
            }, 0);
        }
      })
      .catch((err) => {
        wx.hideLoading();
        console.error("[云函数] [get_user_info] 调用失败", err);
      });
  }

  formatNum(num: number) {
    return parseFloat(String(num)).toLocaleString();
  }
}
</script>

<style lang="scss" scoped>
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
}
.my-wrap {
  font-size: 0;
  font-family: "PingFangSC-Semibold", "PingFang SC";
  background: #fafafa;
  padding: 36rpx 32rpx 80rpx;
  background-size: 100% 576rpx;
  background-repeat: no-repeat;
  background: linear-gradient(
    360deg,
    rgba(171, 213, 255, 0) 0%,
    rgba(171, 213, 255, 0.1) 100%
  );
  .login {
    display: flex;
    align-items: center;
    width: 686rpx;
    height: 120rpx;
    overflow: visible;
  }
  .header {
    position: relative;
    width: 100%;
    margin-bottom: 32rpx;
    margin-top: 34rpx;
    .avatar-img {
      width: 112rpx;
      height: 112rpx;
      border-radius: 50%;
      background: rgba(248, 248, 248, 1);
      box-shadow: 0px 4rpx 16rpx 0px rgba(0, 0, 0, 0.08);
      border: 4rpx solid #ffffff;
      margin-right: 20rpx;
    }
    .header-word {
      white-space: nowrap;
      text-align: left;
      .text-one {
        font-size: 36rpx;
        font-weight: 600;
        color: #333333;
        line-height: 50rpx;
        display: flex;
        align-items: center;
      }
      .text-wrapper {
        margin-top: 6rpx;
        .text-two {
          display: inline-block;
          font-size: 24rpx;
          color: #666666;
          line-height: 34rpx;
          vertical-align: middle;
        }
      }
    }
  }
  .card {
    background-color: #ffffff;
    box-shadow: 0 2rpx 12rpx 0 rgba(0, 0, 0, 0.02);
    border-radius: 20rpx;
    padding: 40rpx 32rpx;
  }
  .info {
    .card-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .title {
        font-size: 32rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #333333;
        line-height: 32rpx;
      }
      .right {
        display: flex;
        align-items: center;
        .red-dot {
          width: 16rpx;
          height: 16rpx;
          border-radius: 50%;
          background: rgba(255, 71, 71, 1);
          margin-right: 8rpx;
        }
        .text {
          font-size: 26rpx;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #999999;
          line-height: 26rpx;
          margin-right: 16rpx;
        }
      }
    }
    .bottom-card {
      margin-top: 32rpx;
      display: flex;
      justify-content: space-between;
      .square {
        width: 296rpx;
        box-sizing: border-box;
        height: 236rpx;
        border-radius: 20rpx;
        padding: 28rpx 24rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .title {
          font-size: 38rpx;
          font-family: PingFangSC-Semibold, PingFang SC;
          font-weight: bold;
          color: #ffffff;
          line-height: 38rpx;
          margin-bottom: 10rpx;
        }
        .subtitle {
          font-size: 50rpx;
          font-family: PingFangSC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          line-height: 22rpx;
          margin-top: 20rpx;
        }
        &.points {
          background: no-repeat right / contain
              url(https://img-pub.oss-cn-beijing.aliyuncs.com/ybcweapp/increase/welfare-miniprogram/invite-bg.png),
            linear-gradient(
              135deg,
              rgba(255, 116, 115, 1) 0%,
              rgba(255, 89, 89, 1) 100%
            );
        }

        &.share {
          background: no-repeat right / contain
              url(https://img-pub.oss-cn-beijing.aliyuncs.com/ybcweapp/increase/welfare-miniprogram/share-bg.png),
            linear-gradient(
              316deg,
              rgba(255, 160, 0, 1) 0%,
              rgba(255, 206, 89, 1) 100%
            );
        }
      }
    }
  }
  .entry-list {
    padding: 0 32rpx;
    margin-top: 32rpx;
    .entry-item {
      box-sizing: border-box;
      width: 622rpx;
      height: 128rpx;
      border-bottom: 1px solid rgba(0, 0, 0, 0.05);
      display: flex;
      align-items: center;
      justify-content: space-between;
      .red-dot {
        width: 16rpx;
        height: 16rpx;
        border-radius: 50%;
        background: rgba(255, 71, 71, 1);
        margin-right: -350rpx;
      }
      &.no-line {
        border-bottom: none;
      }
    }
    .left {
      display: flex;
      align-items: center;
    }
    .icon {
      width: 48rpx;
      height: 48rpx;
      margin-right: 24rpx;
    }
    .title {
      font-size: 32rpx;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #333333;
      line-height: 44rpx;
    }
  }
  .entry-wrap {
    width: 100%;
    font-size: 0px;
    margin-top: 72rpx;
    margin-bottom: 64rpx;
    height: auto;
    .entry-title {
      padding-left: 42rpx;
      font-size: 36rpx;
      font-weight: 500;
      color: #333333;
      line-height: 50rpx;
    }
    .entry-item {
      // margin: 32rpx 114rpx 0 32rpx;
      margin-top: 32rpx;
      display: inline-block;
      width: calc(100% / 3);
      height: auto;
      text-align: center;
      font-size: 0;
      image {
        width: 60rpx;
        height: 60rpx;
      }
      .entry-sub-title {
        margin-top: 12rpx;
        font-size: 26rpx;
        font-weight: 400;
        color: #666666;
        line-height: 36rpx;
      }
    }
  }
}
</style>
